package com.devin.dao;

import com.devin.pojo.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PermissionMapper {

    @Select(" select * from permission where id in(select permissionId from role_permission where roleId=#{roleId})")
    List<Permission> getPermissionsById(Integer roleId);

    @Select(" select * from permission ")
    List<Permission> getPermissions();

    @Insert(" insert into permission values(null,#{permissionName,jdbcType=VARCHAR},#{url,jdbcType=VARCHAR})")
    void addPermission(Permission permission);

    @Update((" delete from role_permission where permissionId=#{permissionId}"))
    void delRoleAndPermission(Integer permissionId);

    @Update(" delete from permission where id=#{permissionId}")
    void delPermission(Integer permissionId);

    @Select(" select * from permission where id not in(select permissionId from role_permission where roleId=#{roleId})")
    List<Permission> getPermissions2Json(Integer roleId);

    @Select(" select * from permission")
    @Results({@Result(id = true,property = "id",column = "id"),
            @Result(property = "permissionName",column = "permissionName"),
            @Result(property = "url",column = "url"),
            @Result(property = "roles",column = "id",javaType = List.class,many = @Many(select = "com.devin.dao.RoleMapper.getRolesByPermissionId"))
    })
    List<Permission> getPermissionsAndRoles();
}
